//
//  PayListVC.m
//  NtoccDriverNew
//
//  Created by niyongsheng on 2022/10/18.
//  Copyright © 2022 GCY. All rights reserved.
//

#import "PayListVC.h"
#import "PayListModel.h"
#import "PayListCell.h"


@interface PayListVC ()
<
UITableViewDelegate,
UITableViewDataSource
>
{
    NSInteger _pageNum;
    NSString *_keyword;
    
    NSString *_startTime;
    NSString *_endTime;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;

@property (weak, nonatomic) IBOutlet UIButton *startTimeBtn;
@property (weak, nonatomic) IBOutlet UIButton *endTimeBtn;
@property (weak, nonatomic) IBOutlet UIButton *clearBtn;

@end

@implementation PayListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"充值记录";
    
    @weakify(self)
    UIImage *riconImg = [[UIImage imageNamed:@"gas_icon_calendar"] imageChangeColor:Theme_ColorValue];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    [rightBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -10 * SP_Width)];
    [rightBtn setImage:riconImg forState:UIControlStateNormal];
    [rightBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        @strongify(self)
        CYBaseSetViewController *bvc = [[CYBaseSetViewController alloc] init];
        [self.navigationController pushViewController:bvc animated:YES];
    }];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView.mj_header beginRefreshing];
}

/// 刷新
/// @param isHeader 是否下拉刷新
- (void)refreshing:(BOOL)isHeader {
    if (isHeader) {
        _pageNum  = 1;
    } else {
        _pageNum ++;
    }
    NSString *urlStr = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GasSpendList);
    NSMutableDictionary *params = @{
        @"pageNum" : @(_pageNum),
        @"pageSize" : @10,
        @"keyword" : _keyword
    }.mutableCopy;
    NSDictionary *branch = [UserDefaultUtil valueForKey:Login_Branch];
    [params setValue:[UserDefaultUtil valueForKey:DriverPartyCode] forKey:@"driverCode"];
    [params setValue:_startTime forKey:@"gmtCreatedStart"];
    [params setValue:_endTime forKey:@"gmtCreatedEnd"];
    [params setValue:self.typeStr forKey:@"energyCardType"];
    @weakify(self)
    [HTTPManager getNetworkRequest:urlStr
                     withParameter:params
                            withVC:self
                           netName:@"充值记录"
                           success:^(id response, NSString *msg) {
        @strongify(self)
        if (isHeader) {
            self.dataSourceMArr = [PayListModel mj_objectArrayWithKeyValuesArray:response[@"records"]];
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView reloadData];
            
        } else {
            NSArray *dictArr = response[@"records"];
            if (dictArr.count > 0) {
                [self.dataSourceMArr addObjectsFromArray:[PayListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
                [self.tableView.mj_footer endRefreshing];
                [self.tableView reloadData];
                
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    } failure:^(NSInteger code, NSString *msg) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - TableView 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellReuseIdentifier = @"PayListCell";
    PayListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSourceMArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, ScreenW, ScreenH - MenuBarHeight - NavHeight - 50) style:UITableViewStylePlain];
        UINib *nib = [UINib nibWithNibName:NSStringFromClass([PayListCell class]) bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:NSStringFromClass([PayListCell class])];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        @weakify(self)
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            [self refreshing:YES];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
            @strongify(self)
            [self refreshing:NO];
        }];
        
        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (IBAction)startTimeBtnOnclicked:(UIButton *)sender {
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = @"选择开始日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        _startTime = [selectValue stringByAppendingFormat:@" 00:00:00"];
        [sender setTitle:selectValue forState:UIControlStateNormal];
        if ([_startTime isNotBlank] && [_endTime isNotBlank]) {
            [self.tableView.mj_header beginRefreshing];
        }
    };
    [datePickerView show:nil];
}

- (IBAction)endTimeBtnOnclicked:(UIButton *)sender {
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]initWithPickerMode:BRDatePickerModeYMD];
    datePickerView.title = @"选择结束日期";
    datePickerView.isAutoSelect = NO;
    datePickerView.showToday = NO;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        _endTime = [selectValue stringByAppendingFormat:@" 23:59:59"];
        [sender setTitle:selectValue forState:UIControlStateNormal];
        if ([_startTime isNotBlank] && [_endTime isNotBlank]) {
            [self.tableView.mj_header beginRefreshing];
        }
    };
    [datePickerView show:nil];
}

- (IBAction)clearBtnOnclicked:(UIButton *)sender {
    [self.startTimeBtn setTitle:@"开始时间" forState:UIControlStateNormal];
    [self.endTimeBtn setTitle:@"结束时间" forState:UIControlStateNormal];
    _startTime = nil;
    _endTime = nil;
    [self.tableView.mj_header beginRefreshing];
}


@end

